package com.platform.mobile.operator;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.platform.base.SysException;
import com.platform.mobile.bean.AddCourseRequest;
import com.platform.mobile.bean.CourseResBean;
import com.platform.mobile.bean.ReqBean;
import com.platform.mobile.bean.ResBean;
import com.platform.mobile.bean.ResultBean;
import com.platform.mobile.factory.Operator;
import com.platform.mobile.service.PaitentService;

@Component
public class AddCourse extends Operator{
	
	private static Logger log = LoggerFactory.getLogger(AddCourse.class);

	@Autowired
	private PaitentService service;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResBean operate(ReqBean reqBean) throws SysException {
		
		log.info("进入到AddCourse业务处理类，处理[" + reqBean.getClientInfo() + "]用户请求");
		ResBean resBean = new ResBean();
		resBean.setClientInfo(reqBean.getClientInfo());
		resBean.setJsonrpc("2.0");
		resBean.setMethod("AddCourse");
		ResultBean result = new ResultBean();
		
		AddCourseRequest request = new AddCourseRequest();
		
		try{
			
			Object params = reqBean.getParams();
			if(params != null){
				
				LinkedHashMap<String, Object> paramMap = (LinkedHashMap<String, Object>)params;
				request.setContent(paramMap.get("content").toString());
				Date date = null;
				
				try{
					date = new Date(paramMap.get("date").toString());
				}catch(Exception e){
					e.printStackTrace();
					date = new Date(System.currentTimeMillis());
				}
				
				
				request.setDate(date);
				request.setName(paramMap.get("name").toString());
				request.setRecord_id((int)paramMap.get("record_id"));
				
				ArrayList<Object> resArray
					= (ArrayList<Object>)paramMap.get("pictures");
				
				for(Object obj : resArray){
					LinkedHashMap<String, Object> res
						= (LinkedHashMap<String, Object>) obj;
					CourseResBean bean = new CourseResBean();
					bean.setPicture(res.get("picture").toString());
					
					request.getPicture().add(bean);
				}
				
				service.addCourse(request);
				
				result.setResultStatus("1");
				result.setInfo("添加病程成功");
				result.setData(null);
			}
			
		}catch(Exception e){
			e.printStackTrace();
			result.setResultStatus("0");
			result.setInfo("服务器异常");
			result.setData(null);
		}
		
		resBean.setResult(result);
		return resBean;
	}

}
